<?php
function getarea(){
		require './lib/qqwry/qqwry.php';
		
		mb_internal_encoding("UTF-8");
		
		global $link;
		
		$QQWry=new QQWry;
		
		$ip=getip();
		$ifErr=$QQWry->QQWry($ip);
		
		$area = mb_convert_encoding( $QQWry->Country, 'UTF-8','GBK' );
		file_put_contents('./lib/qqwry/all.log',$area."\r\n",FILE_APPEND);
		
		if ( strstr( $area,'上海' ) ){
			$city =  "上海";
		}else if ( strstr( $area,'北京' ) ){
			$city =  "北京";
		}else if ( strstr( $area,'天津' ) ){
			$city =  "天津";
		}else if ( strstr( $area,'重庆' ) ){
			$city =  "重庆";
		}else {
			$step = mb_strpos( $area, '省',0,'UTF-8' );
			if ( !$step ){
				$step = 0;
			}

			$step2 = mb_strpos( $area, '市',0,'UTF-8' );
			if ( !$step2 ){
				$step2 = mb_strlen( $area );
			}

			$city = mb_substr( $area, $step+1, $step2, 'UTF-8');
			if ( empty( $city ) ){
				$city = '上海';
				file_put_contents('./lib/qqwry/nofind.log',$city."\r\n",FILE_APPEND);
			}else {
				file_put_contents('./lib/qqwry/suc.log',$city."\r\n",FILE_APPEND);
			}
		}
		
		$area = mb_substr($city,0,2);	
		$condition = "`name` LIKE '%{$area}%'";
	
	if ( !empty($area)  ){
		$sql = "SELECT *FROM `pre_common_district` WHERE level=2 and {$condition}";
		$rs = mysql_query($sql);
		$f_area = fetch_all( $rs );
		
		if ( empty( $f_area ) ){
			file_put_contents('./lib/qqwry/nodb.log',$city."\t".$sql."\r\n",FILE_APPEND);
			
			$sql = "SELECT *FROM `pre_common_district` WHERE level=2 and `name` LIKE '%上海%'";
			$rs = mysql_query($sql);
			$f_area = fetch_all( $rs );	
		}
		
		if ( !empty($f_area[0]) ){
			$sql = "SELECT *FROM `pre_common_district` WHERE upid = {$f_area[0]['id']}";
			$rs = mysql_query($sql);
			$s_area = fetch_all( $rs );
			
			$areas = array_merge( $f_area, $s_area );
		}
		return $areas;
	}else{
		return '';
	}
}
function getip(){//获取IP
    return $_SERVER[REMOTE_ADDR]?$_SERVER[REMOTE_ADDR]:$GLOBALS[HTTP_SERVER_VARS][REMOTE_ADDR];
}
function updatekey( $arr, $k ){
	$newarr = array();
	foreach ( $arr as $a ){
		$newarr[$a[$k]] = $a;
	}
	return $newarr;	
}